//引入angular核心组件
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
//引入类
import { Room } from '../../domain/room';
import { RoomType } from '../../domain/roomType';
//引入服务
import { RoomService } from '../../service/room.service';
import { RoomTypeService } from '../../service/room-type.service';

import { ROOMSTATUS } from '../../domain/myEnum';

declare var layui: any;
@Component({
  selector: 'app-room-list',
  templateUrl: './room-list.component.html',
  styleUrls: ['./room-list.component.css'],
  providers: [RoomService, RoomTypeService]
})
export class RoomListComponent implements OnInit {

  room: Room;
  rooms: Room[];
  selectedRoomId: number;

  roomStatus: string[];

  roomTypes: RoomType[];

  constructor(
    private roomService: RoomService,
    private roomTypeService: RoomTypeService,
    private router: Router
  ) { }

  ngOnInit() {
    layui.use(['element', 'layer', 'form'], function () {
      var element = layui.element;
      var layer = layui.layer;
      var form = layui.form;
      element.render('element');
      form.render();
      //监听折叠
      element.on('collapse(newRoom)', function (data) {
        layer.msg('展开状态：' + data.show);
      });
    });

    //组件加载初期查询所有房间
    this.roomService.getRooms()
      .then(res => this.rooms = res);

    this.roomTypeService.getRoomTypes()
      .then(res => this.roomTypes = res);

    this.room = new Room();
    this.roomStatus = ROOMSTATUS;

  }

  //选择一个房间
  isSelect(room: Room) {
    return room.id === this.selectedRoomId;
  }

  //路由到选择房间的详细页面
  onSelect(room: Room) {
    this.router.navigate(['/room-detail', room.id]);
  }

  deleteRoom(room: Room) {
    this.roomService.deleteRoom(room)
      .then(() => {
        location.reload();
      })
  }

  createRoom(): void {
    this.roomService.createRoom(this.room)
      .then(res => {
        console.log(res);
      })
  }

  orderRoom(room: Room){
    room.status='FULL';
  }

  cancelRoom(room:Room){
    room.status='EMPTY';
  }

}
